import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index'
import Admin from '@/layout/admin'
import Layout from '@/layout/index'
Vue.use(Router)

// 公共路由
export const constantRoutes = [
];

// 路由对象
const router = new Router({
  mode: 'history',
  routes: [
    // 路由规则
    // {
    //   path: '/login',
    //   component: (resolve) => require(['@/layout/views/login'], resolve),
    //   hidden: true
    // },
    {
      path: '/admin',
      component: Admin,
      redirect: 'index',
      children: [
        {
          path: 'article/new',
          component: (resolve) => require(['@/layout/views/admin/article/new'], resolve),
        }
      ]
    },
    //////////////////////////////////////
    {
      path: '/',
      component: (resolve) => require(['@/layout/views/bbs/bbsLayout'], resolve),
      redirect: '/',
      children: [
        {
          path: '/archives/make',
          component: (resolve) => require(['@/layout/views/bbs/article/make'], resolve),
          name: '写文章',
          meta: { title: '写文章'}
        },
        {
          path: '/archives/:id',
          component: (resolve) => require(['@/layout/views/bbs/article/article'], resolve),
          name: '文章',
          meta: { title: '文章'},
        },
        {
          path: '/space/:id',
          component: (resolve) => require(['@/layout/views/bbs/space/index'], resolve),
          meta: { title: '用户中心'},
          redirect: '/space/:id/archive',
          children: [
            {
              path: 'archive',
              component: (resolve) => require(['@/layout/views/bbs/space/sub/archive'], resolve),
              meta: { title: '用户中心-投稿'},
            },
            {
              path: 'comment',
              component: (resolve) => require(['@/layout/views/bbs/space/sub/comment'], resolve),
              meta: { title: '用户中心-评论'},
            },
            {
              path: 'favor',
              component: (resolve) => require(['@/layout/views/bbs/space/sub/favor'], resolve),
              meta: { title: '用户中心-收藏'},
            }
          ]
        },
        {
          path: '/user/setting',
          component: (resolve) => require(['@/layout/views/bbs/user/setting'], resolve),
          name: '用户中心',
          meta: { title: '用户中心'},
        },
        {
          path: '/',
          component: (resolve) => require(['@/layout/views/bbs/article/list'], resolve),
          name: '首页',
          meta: { title: '首页'}
        },
      ]
    },
    //////////////////////////////////////
    {
      path: '/404',
      component: (resolve) => require(['@/layout/views/util/404'], resolve),
      hidden: true
    },
    {
      path: '/401',
      component: (resolve) => require(['@/layout/views/util/401'], resolve),
      hidden: true
    },
    {
      path: '/test',
      component: (resolve) => require(['@/layout/views/admin/test/test1'], resolve),
      hidden: true
    },
    // {
    //   path: '/admin',
    //   component: Admin,
    //   redirect: 'index',
    //   children: [
    //     {
    //       path: 'index',
    //       component: (resolve) => require(['@/layout/views/index'], resolve),
    //       name: '管理页面',
    //       meta: { title: '管理页面', icon: 'dashboard', noCache: true, affix: true }
    //     }
    //   ]
    // },
  ]
});
// 路由请求处理器
router.beforeEach((to, from, next) => {//beforeEach是router的钩子函数，在进入路由前执行
  // 旧请求取消
  store.state.axiosPromiseArr.forEach((ele, index) => {
    ele.cancel();
    delete store.state.axiosPromiseArr[index]
  });
  // 判断是否有标题，存在时修改页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  } else if(store.state.routerTitle){
    document.title = store.state.routerTitle;
    store.state.routerTitle = null;
  } else {
    if(document.title != store.getters.config.DEFAULT_TITLE) document.title = store.getters.config.DEFAULT_TITLE;
  }
  next()//执行进入路由，如果不写就不会进入目标页
});
// 输出对象
export default router;
